<?php

class token
{

    public function __construct()
    {
        $CI = & get_instance();
        $this->db = $CI->db;
        $this->router = $CI->router;
        $CI->load->library('cache');
        $this->cache = $CI->cache;
    }

    public function user()
    {
        $token = $_REQUEST['token'];
        if (! empty($token))
            $this->user = array();
        return $this->getUser($token);
    }
    
    // 通过token获取用户信息
    public function getUser($token)
    {
        $detail = $this->cache->get($token);
        if (CACHE_FLAG || $detail === false ) {
            $result = $this->db->select('cache')
                ->where('token', $token)
                ->get(TABLE_MEMBER_TOKEN)
                ->row_array(0);
            $detail = $result['cache'];
            $this->cache->set($token, $detail, false, 3600 * 24);
        }
        
        $detail = json_decode($detail, true);
        return $detail;
    }
}